home *** CD-ROM | disk | FTP | other *** search
/ Amiga Magazin: Amiga-CD 1996 March / Amiga-CD 1996 #3.iso / pd-software / mui_3.1 / developer / c / examples / layout.c < prev    next >
C/C++ Source or Header  |  1996-01-19  |  5KB  |  216 lines

  1. #include "demo.h"
  2. #include <time.h>
  3.  
  4. #define ID_REWARD 1
  5.  
  6.  
  7. /*
  8. ** Custom layout function.
  9. ** Perform several actions according to the messages lm_Type
  10. ** field. Note that you must return MUILM_UNKNOWN if you do
  11. ** not implement a specific lm_Type.
  12. */
  13.  
  14. SAVEDS ULONG __asm LayoutFunc(REG(a0) struct Hook *h,REG(a2) Object *obj,REG(a1) struct MUI_LayoutMsg *lm)
  15. {
  16.     switch (lm->lm_Type)
  17.     {
  18.         case MUILM_MINMAX:
  19.         {
  20.             /*
  21.             ** MinMax calculation function. When this is called,
  22.             ** the children of your group have already been asked
  23.             ** about their min/max dimension so you can use their
  24.             ** dimensions to calculate yours.
  25.             **
  26.             ** In this example, we make our minimum size twice as
  27.             ** big as the biggest child in our group.
  28.             */
  29.  
  30.             Object *cstate = (Object *)lm->lm_Children->mlh_Head;
  31.             Object *child;
  32.  
  33.             WORD maxminwidth  = 0;
  34.             WORD maxminheight = 0;
  35.  
  36.             /* find out biggest widths & heights of our children */
  37.  
  38.             while (child = NextObject(&cstate))
  39.             {
  40.                 if (maxminwidth <MUI_MAXMAX && _minwidth (child) > maxminwidth ) maxminwidth  = _minwidth (child);
  41.                 if (maxminheight<MUI_MAXMAX && _minheight(child) > maxminheight) maxminheight = _minheight(child);
  42.             }
  43.  
  44.             /* set the result fields in the message */
  45.  
  46.             lm->lm_MinMax.MinWidth  = 2*maxminwidth;
  47.             lm->lm_MinMax.MinHeight = 2*maxminheight;
  48.             lm->lm_MinMax.DefWidth  = 4*maxminwidth;
  49.             lm->lm_MinMax.DefHeight = 4*maxminheight;
  50.             lm->lm_MinMax.MaxWidth  = MUI_MAXMAX;
  51.             lm->lm_MinMax.MaxHeight = MUI_MAXMAX;
  52.  
  53.             return(0);
  54.         }
  55.  
  56.         case MUILM_LAYOUT:
  57.         {
  58.             /*
  59.             ** Layout function. Here, we have to call MUI_Layout() for each
  60.             ** our children. MUI wants us to place them in a rectangle
  61.             ** defined by (0,0,lm->lm_Layout.Width-1,lm->lm_Layout.Height-1)
  62.             ** You are free to put the children anywhere in this rectangle.
  63.             **
  64.             ** If you are a virtual group, you may also extend
  65.             ** the given dimensions and place your children anywhere. Be sure
  66.             ** to return the dimensions you need in lm->lm_Layout.Width and
  67.             ** lm->lm_Layout.Height in this case.
  68.             **
  69.             ** Return TRUE if everything went ok, FALSE on error.
  70.             ** Note: Errors during layout are not easy to handle for MUI.
  71.             **       Better avoid them!
  72.             */
  73.  
  74.             Object *cstate = (Object *)lm->lm_Children->mlh_Head;
  75.             Object *child;
  76.  
  77.             while (child = NextObject(&cstate))
  78.             {
  79.                 LONG mw = _minwidth (child);
  80.                 LONG mh = _minheight(child);
  81.                 LONG l = rand() % (lm->lm_Layout.Width  - mw);
  82.                 LONG t = rand() % (lm->lm_Layout.Height - mh);
  83.  
  84.                 if (!MUI_Layout(child,l,t,mw,mh,0))
  85.                     return(FALSE);
  86.             }
  87.  
  88.             return(TRUE);
  89.         }
  90.     }
  91.     return(MUILM_UNKNOWN);
  92. }
  93.  
  94.  
  95. SAVEDS ULONG __asm PressFunc(REG(a0) struct Hook *h,REG(a2) Object *app,REG(a1) ULONG *num)
  96. {
  97.     static int lastnum = -1;
  98.  
  99.     lastnum++;
  100.  
  101.     if (lastnum!=*num)
  102.     {
  103.         DisplayBeep(0);
  104.         lastnum = -1;
  105.     }
  106.     else if (lastnum==7)
  107.     {
  108.         DoMethod(app,MUIM_Application_ReturnID,ID_REWARD);
  109.         lastnum = -1;
  110.     }
  111.  
  112.     return(0);
  113. }
  114.  
  115.  
  116. int main(int argc,char *argv[])
  117. {
  118.     APTR app,window;
  119.     APTR b[8],yeah;
  120.     static struct Hook LayoutHook = { {0,0}, LayoutFunc, NULL, NULL };
  121.     static struct Hook PressHook  = { {0,0}, PressFunc , NULL, NULL };
  122.     ULONG signals;
  123.     BOOL running = TRUE;
  124.     int i;
  125.  
  126.     init();
  127.  
  128.     srand(time(0));
  129.  
  130.     app = ApplicationObject,
  131.         MUIA_Application_Title      , "Layout",
  132.         MUIA_Application_Version    , "$VER: Layout 12.10 (21.11.95)",
  133.         MUIA_Application_Copyright  , "©1993, Stefan Stuntz",
  134.         MUIA_Application_Author     , "Stefan Stuntz",
  135.         MUIA_Application_Description, "Demonstrate custom layout hooks.",
  136.         MUIA_Application_Base       , "Layout",
  137.  
  138.         SubWindow, window = WindowObject,
  139.             MUIA_Window_Title, "Custom Layout",
  140.             MUIA_Window_ID   , MAKE_ID('C','L','S','3'),
  141.             WindowContents, VGroup,
  142.  
  143.                 Child, TextObject,
  144.                     TextFrame,
  145.                     MUIA_Background, MUII_TextBack,
  146.                     MUIA_Text_Contents, "\33cDemonstration of a custom layout hook.\nSince it's usually no good idea to have overlapping\nobjects, your hooks should be more sophisticated.",
  147.                     End,
  148.  
  149.                 Child, VGroup,
  150.                     GroupFrame,
  151.                     MUIA_Group_LayoutHook, &LayoutHook,
  152.                     Child, b[0] = SimpleButton("Click"),
  153.                     Child, b[1] = SimpleButton("me"),
  154.                     Child, b[2] = SimpleButton("in"),
  155.                     Child, b[3] = SimpleButton("correct"),
  156.                     Child, b[4] = SimpleButton("sequence"),
  157.                     Child, b[5] = SimpleButton("to"),
  158.                     Child, b[6] = SimpleButton("be"),
  159.                     Child, b[7] = SimpleButton("rewarded!"),
  160.                     Child, yeah = SimpleButton("Yeah!\nYou did it!\nClick to quit!"),
  161.                     End,
  162.  
  163.                 End,
  164.  
  165.             End,
  166.         End;
  167.  
  168.     if (!app)
  169.         fail(app,"Failed to create Application.");
  170.  
  171.     DoMethod(window,MUIM_Notify,MUIA_Window_CloseRequest,TRUE,
  172.         app,2,MUIM_Application_ReturnID,MUIV_Application_ReturnID_Quit);
  173.  
  174.     DoMethod(yeah,MUIM_Notify,MUIA_Pressed,FALSE,
  175.         app,2,MUIM_Application_ReturnID,MUIV_Application_ReturnID_Quit);
  176.  
  177.     for (i=0;i<8;i++)
  178.         DoMethod(b[i],MUIM_Notify,MUIA_Pressed,FALSE,
  179.             app,3,MUIM_CallHook,&PressHook,i);
  180.  
  181.     set(yeah,MUIA_ShowMe,FALSE);
  182.  
  183.  
  184. /*
  185. ** Input loop...
  186. */
  187.  
  188.     set(window,MUIA_Window_Open,TRUE);
  189.  
  190.     while (running)
  191.     {
  192.         switch (DoMethod(app,MUIM_Application_Input,&signals))
  193.         {
  194.             case MUIV_Application_ReturnID_Quit:
  195.                 running = FALSE;
  196.                 break;
  197.  
  198.             case ID_REWARD:
  199.                 set(yeah,MUIA_ShowMe,TRUE);
  200.                 break;
  201.         }
  202.  
  203.         if (running && signals) Wait(signals);
  204.     }
  205.  
  206.     set(window,MUIA_Window_Open,FALSE);
  207.  
  208.  
  209. /*
  210. ** Shut down...
  211. */
  212.  
  213.     MUI_DisposeObject(app);     /* dispose all objects. */
  214.     fail(NULL,NULL);            /* exit, app is already disposed. */
  215. }
  216.